from django.urls import reverse
from django.shortcuts import redirect
from django.views.generic import ListView, CreateView, DeleteView, UpdateView
from .models import ToDoItem


class ToDoListView(ListView):
    model = ToDoItem
    template_name = "todo_app/index.html"


class ItemCreate(CreateView):
    model = ToDoItem
    fields = [
        "title",
        "description"
    ]

    def get_context_data(self):
        context = super(ItemCreate, self).get_context_data()
        context["title"] = "Добавьте новую позицию"
        return context

    def get_success_url(self):
        return reverse("index")

class ItemDelete(DeleteView):
    model = ToDoItem

    def get_success_url(self):
        return reverse("index")


def change_status(request, pk):
    item = ToDoItem.objects.get(pk=pk)
    if item.is_done:
        item.is_done = False
    else:
        item.is_done = True

    item.save()
    return redirect('index')